package com.cgsd.heyu.ui.info;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.BaseAdapter;
import com.android.library.adapter.ClickItemListener;
import com.android.library.adapter.ViewHolder;
import com.android.library.utils.ArithUtil;
import com.android.library.utils.BeanUtils;
import com.android.library.utils.StringUtils;
import com.android.library.wedgets.TitleBarView;
import com.cgsd.heyu.R;
import com.cgsd.heyu.ad.AdInputScreen;
import com.cgsd.heyu.ad.AdListener;
import com.cgsd.heyu.common.AdConstants;
import com.cgsd.heyu.entity.AdContentEntity;
import com.cgsd.heyu.entity.CashOutAccountEntity;
import com.cgsd.heyu.entity.CashOutProductEntity;
import com.cgsd.heyu.entity.FlowerInfoEntity;
import com.cgsd.heyu.entity.TaskEntity;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.utils.FormatUtil;
import com.cgsd.heyu.utils.StatusBarUtil;
import com.cgsd.heyu.widgets.dialog.InputCashOutDialog;
import com.cgsd.heyu.widgets.dialog.ShareRewardDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 说明
 * CreateData: 2021/1/27
 */
public class CashOutActivity extends BaseActivity<HomeContract.Presenter> implements ClickItemListener, HomeContract.CashOutView, View.OnClickListener, TitleBarView.TitleBarClickListener {

    //微信提现
    public static final String CASH_OUT_WEIXIN = "2";
    //支付宝提现
    public static final String CASH_OUT_ALIPAY = "1";
    private List<CashOutProductEntity> data = new ArrayList<>();
    private BaseAdapter<CashOutProductEntity> adapter;
    private int currentPosition;
    //提现方式
    private String cashOutType;
    private InputCashOutDialog inputCashOutDialog;
    private String userTotal;

    private TextView mTvIntegralNum;
    private RelativeLayout mLayoutWeixinPay;
    private RelativeLayout mLayoutWeixinBind;
    private TextView mTvBindWeixin;
    private TextView mTvBindWeixinName;
    private RelativeLayout mLayoutAlipay;
    private RelativeLayout mLayoutAlipayBind;
    private TextView mTvBindAlipay;
    private TextView mTvBindAlipayName;
    private TextView mTvNeedIntegral;

    public static void actionStart(Activity activity, FlowerInfoEntity flowerInfoEntity) {
        Intent intent = new Intent(activity, CashOutActivity.class);
        intent.putExtra("flowerInfoEntity", flowerInfoEntity);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cash_out;
    }

    @Override
    protected void initView() {
        new HomePresenter(this);
        mTitleBar = findViewById(R.id.title_bar);
        mTvIntegralNum = findViewById(R.id.tv_integral_num);
        mLayoutWeixinPay = findViewById(R.id.layout_weixin_pay);
        mLayoutWeixinBind = findViewById(R.id.layout_weixin_bind);
        mTvBindWeixin = findViewById(R.id.tv_bind_weixin);
        mTvBindWeixinName = findViewById(R.id.tv_bind_weixin_name);
        mLayoutAlipay = findViewById(R.id.layout_alipay);
        mLayoutAlipayBind = findViewById(R.id.layout_alipay_bind);
        mTvBindAlipay = findViewById(R.id.tv_bind_alipay);
        mTvBindAlipayName = findViewById(R.id.tv_bind_alipay_name);
        mTvNeedIntegral = findViewById(R.id.tv_need_integral);
        selectCashOutWay(CASH_OUT_ALIPAY);

        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        adapter = new BaseAdapter<CashOutProductEntity>(this, R.layout.item_cash_out_product, data) {
            @SuppressLint("SetTextI18n")
            @Override
            public void convert(ViewHolder holder, CashOutProductEntity entity, int position) {
                RelativeLayout layoutRoot = holder.getView(R.id.root);
                TextView tvRecommend = holder.getView(R.id.tv_recommend);
                TextView tvPrice = holder.getView(R.id.tv_price);
                TextView tvYuan = holder.getView(R.id.tv_yuan);
                TextView tvIntegral = holder.getView(R.id.tv_integral_num);
                tvPrice.setText(FormatUtil.getPrice(entity.getWithdrawAmount()));
                tvIntegral.setText(entity.getFlowerNum() + mContext.getString(R.string.a_flower));
                if (currentPosition == position) {
                    layoutRoot.setBackgroundResource(R.drawable.border_blue_solid_blue);
                    tvPrice.setTextColor(Color.parseColor("#33B9CC"));
                    tvYuan.setTextColor(Color.parseColor("#33B9CC"));
                    tvIntegral.setTextColor(Color.parseColor("#33B9CC"));
                } else {
                    layoutRoot.setBackgroundResource(R.drawable.border_gray_solid_white);
                    tvPrice.setTextColor(Color.parseColor("#333333"));
                    tvYuan.setTextColor(Color.parseColor("#333333"));
                    tvIntegral.setTextColor(Color.parseColor("#999999"));
                }
                if (CashOutProductEntity.RECOMMEND.equals(entity.getRecommendFlag())) {
                    tvRecommend.setVisibility(View.VISIBLE);
                } else {
                    tvRecommend.setVisibility(View.GONE);
                }
            }
        };
        mRecyclerView.setAdapter(adapter);
        adapter.setClickItemListener(this);
        mLayoutWeixinPay.setOnClickListener(this);
        mLayoutAlipay.setOnClickListener(this);
        mTvBindAlipay.setOnClickListener(this);
        mTvBindWeixin.setOnClickListener(this);
        mTitleBar.setTitleBarClickListener(this);
        findViewById(R.id.tv_submit).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        FlowerInfoEntity entity = getIntent().getParcelableExtra("flowerInfoEntity");
        if (entity == null) {
            mTvIntegralNum.setText("0");
            userTotal = "0";
        } else {
            userTotal = entity.usableAmount;
            mTvIntegralNum.setText(entity.usableAmount);
        }
        mPresenter.cashOutProduct();
        mPresenter.cashOutAccountInfo();
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.fullBar(this);
    }

    @Override
    public void clickItem(View view, int position) {
        if (position != currentPosition) {
            currentPosition = position;
            adapter.notifyDataSetChanged();
            mTvNeedIntegral.setText(data.get(position).getFlowerNum());
        }
    }

    @Override
    public void cashOutProduct(List<CashOutProductEntity> entities) {
        if (BeanUtils.isEmpty(entities)) {
            return;
        }
        mTvNeedIntegral.setText(entities.get(0).getFlowerNum());
        data.clear();
        data.addAll(entities);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void bindSuccess() {
        showToast("绑定成功");
        mPresenter.cashOutAccountInfo();
    }

    @Override
    public void cashOutAccountInfo(CashOutAccountEntity entity) {
        dismissCommonSubmitDialog();
        if (StringUtils.isNotEmpty(entity.getAliWithdrawName())) {
            mTvBindAlipay.setVisibility(View.GONE);
            mTvBindAlipayName.setVisibility(View.VISIBLE);
            mTvBindAlipayName.setText(entity.getAliWithdrawName());
        } else {
            mTvBindAlipay.setVisibility(View.VISIBLE);
            mTvBindAlipayName.setVisibility(View.GONE);
        }
        if (StringUtils.isNotEmpty(entity.getWxWithdrawName())) {
            mTvBindWeixin.setVisibility(View.GONE);
            mTvBindWeixinName.setVisibility(View.VISIBLE);
            mTvBindWeixinName.setText(entity.getWxWithdrawName());
        } else {
            mTvBindWeixin.setVisibility(View.VISIBLE);
            mTvBindWeixinName.setVisibility(View.GONE);
        }
    }

    private ShareRewardDialog shareRewardDialog;

    @Override
    public void cashOutSuccess() {
        dismissCommonSubmitDialog();
        showToast("申请提现成功");
//        String total = ArithUtil.sub(userTotal, data.get(currentPosition).getFlowerNum(), 0);
//        userTotal = total;
//        mTvIntegralNum.setText(userTotal);

        if (shareRewardDialog == null) {
            shareRewardDialog = new ShareRewardDialog(this, AdConstants.TASK_REWORD_BANNER);
        }
        shareRewardDialog.setCashTip();
        shareRewardDialog.show();


    }

    /**
     * 选择提现方式
     */
    private void selectCashOutWay(String payWay) {
        cashOutType = payWay;
        if (CASH_OUT_ALIPAY.equals(cashOutType)) {
            mLayoutWeixinPay.setBackgroundResource(R.drawable.border_gray_solid_white);
            mLayoutAlipay.setBackgroundResource(R.drawable.border_blue_solid_blue);
            mLayoutWeixinBind.setVisibility(View.GONE);
            mLayoutAlipayBind.setVisibility(View.VISIBLE);
        } else {
            mLayoutWeixinPay.setBackgroundResource(R.drawable.border_blue_solid_blue);
            mLayoutAlipay.setBackgroundResource(R.drawable.border_gray_solid_white);
            mLayoutWeixinBind.setVisibility(View.VISIBLE);
            mLayoutAlipayBind.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.layout_weixin_pay) {
            //选择微信支付
            selectCashOutWay(CASH_OUT_WEIXIN);
        } else if (v.getId() == R.id.layout_alipay) {
            //选择支付宝支付
            selectCashOutWay(CASH_OUT_ALIPAY);
        } else if (v.getId() == R.id.tv_bind_weixin) {
            //bind微信
            showBindDialog();
        } else if (v.getId() == R.id.tv_bind_alipay) {
            //bind支付宝
            showBindDialog();
        } else if (v.getId() == R.id.tv_submit) {
            //提现
            submitData();
        }
    }


    /**
     * 绑定dialog
     */
    private void showBindDialog() {
        if (inputCashOutDialog == null) {
            inputCashOutDialog = new InputCashOutDialog(this);
            inputCashOutDialog.setDialogListener(new InputCashOutDialog.DialogListener() {
                @Override
                public void confirm(String name, String account) {
                    inputCashOutDialog.dismiss();
                    showCommonSubmitDialog("");
                    mPresenter.cashOutAccountBind(name, account, cashOutType);
                }

                @Override
                public void cancel() {
                    inputCashOutDialog.dismiss();
                }
            });
        }
        inputCashOutDialog.setType(cashOutType);
        inputCashOutDialog.show();
    }

    @Override
    public void leftClick() {
        finish();
    }

    @Override
    public void rightClick() {
        CashOutRecordActivity.actionStart(this);
    }

    /**
     * 提现提交数据
     */
    private void submitData() {
        if (BeanUtils.isEmpty(data)) {
            showToast("数据错误");
            return;
        }
        showCommonSubmitDialog("");
        mPresenter.cashOut(data.get(currentPosition).getSeqId(), cashOutType);
    }
}
